package com.zzld.modules.sys.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.zzld.core.service.BaseServiceImpl;
import com.zzld.core.utils.UuidUtil;
import com.zzld.modules.sys.entity.UserRole;
import com.zzld.modules.sys.mapper.UserRoleMapper;
import com.zzld.modules.sys.service.IUserRoleService;

@Service("userRoleService")
public class UserRoleServiceImpl  extends BaseServiceImpl<UserRole> implements IUserRoleService {

	@Autowired
	private UserRoleMapper userroleMapper;

	
	public List<String> selectbyrole(String user_id){
		
	    List<String> r = userroleMapper.selectbyrole(user_id);
		return r;
	}
	
	public Integer selectbyusid(String id){
		return userroleMapper.selectbyusid(id);
	}
	
	public Integer selectbyroleid(String id){
		return userroleMapper.selectbyroleid(id);
	}
	
	/*
	 * 修改用户角色
	 */
	public int updatejiaose(String id, String ids) {
		List<UserRole> list = userroleMapper.selectbyuserid(id);
		byte q = 0;
		if (list != null) {
			for (UserRole r : list) {
				if (userroleMapper.deleteByPrimaryKey(r.getId()) == 0) {
					q = 1;
					break;
				}
			}
		}

		UserRole ur = new UserRole();
		for (String i : ids.split(",")) {
			ur.setId(UuidUtil.uuid().toString());
			ur.setRoleId(i);
			ur.setUserId(id);
			if (userroleMapper.insert(ur) == 0) {
				q = 1;
				break;
			}
		}
		if (q == 0) {
			return 1;
		} else {
			return 0;
		}
	}

}
